#!/usr/bin/env bash

### BEGIN ###
# Author: idevz
# Since: 2018/03/12
# Description:   An entrypoint script in the parallels vm.
# prlctl exec golang 'sudo -Hiu z set_up'
### END ###

set -e

[ "${HOME}" != '/home/z' ] && echo 'Are U Z ?' && exit 1
HOSTNAME=$(hostname)
PRLCTL_HOME=${PRLCTL_HOME:-"/media/psf/runX"}
# https://github.com/koalaman/shellcheck
# shellcheck disable=2046
BASE_DIR=$(dirname $(cd $(dirname "$0") && pwd -P)/$(basename "$0"))

xnotic() {
	STATUS=1
	[ ! -z "${2}" ] && STATUS="${2}"
	echo
	echo "${1}" && echo && exit "${STATUS}"
}

ynotic() {
	echo
	echo "${1}"
}

# create a profile for each host such as envriment variable setting
create_profile() {
	[ -z "${1}" ] && echo "empty profile content." && exit 1
	PROFILE_FILE="/etc/profile.d/idevz_prlctl_${HOSTNAME}.sh"
	[ ! -z "${2}" ] &&
		PROFILE_FILE="/etc/profile.d/idevz_prlctl_${2}.sh"
	[ -f "${PROFILE_FILE}" ] && sudo rm "${PROFILE_FILE}"
	echo "${1}" | sudo tee -a "${PROFILE_FILE}"
	# sudo sh -c "echo zzz >> /etc/profile"
	sudo chmod +x "${PROFILE_FILE}"
}

check_file() {
	FILE_NAME="${1}"
	FILE_DIR=$(dirname "${FILE_NAME}")
	[ ! -d "${FILE_DIR}" ] && mkdir -p "${FILE_DIR}"
	[ ! -f "${FILE_NAME}" ] && touch "${FILE_NAME}"
	ynotic "${FILE_NAME} checked."
}

init_etc_profile() {
	# shellcheck disable=2002
	cat "${PRLCTL_HOME}/shell/etc-profile" | sudo tee /etc/profile >/dev/null 2>&1
	cat "${PRLCTL_HOME}/shell/etc-sudoers" | sudo tee /etc/sudoers >/dev/null 2>&1
}

init_zsh() {
	HISTFILE="${PRLCTL_HOME}/shell/$(hostname)${HOME}/.zsh_history"
	ROOT_HISTFILE="${PRLCTL_HOME}/shell/$(hostname)/root/.zsh_history"
	cp -f "${PRLCTL_HOME}/shell/runX.zshrc" ~/.zshrc
	# shellcheck disable=2002
	cat "${PRLCTL_HOME}/shell/runX.zshrc" | sudo tee /root/.zshrc >/dev/null 2>&1
	check_file "${HISTFILE}"
	check_file "${ROOT_HISTFILE}"
}

init_gdb() {
	GDB_HISTORY_FILE="${PRLCTL_HOME}/gdb/$(hostname)/.gdb_history"
	GDB_BREAKPOINTS_FILE="${PRLCTL_HOME}/gdb/$(hostname)/$(whoami).bps"
	GDB_INIT_FILE_NAME="${PRLCTL_HOME}/gdb/$(hostname)/.gdbinit"
	check_file "${GDB_HISTORY_FILE}"
	check_file "${GDB_BREAKPOINTS_FILE}"
	check_file "${GDB_INIT_FILE_NAME}"
	GDB_INIT_CONTENT=$(
		cat <<GDB_INIT
set history save on
set history filename ${GDB_HISTORY_FILE}
source ${PRLCTL_HOME}/gdb/ext/gdb-dashboard/.gdbinit

define sbps
	save breakpoints ${GDB_BREAKPOINTS_FILE}
	print "save the breakpoints to \"%s\"", "${GDB_BREAKPOINTS_FILE}"
end
document sbps
    save break points
    Usage: sbps
end

define rbps
	source ${GDB_BREAKPOINTS_FILE}
end
document rbps
    load the break points
    Usage: rbps
end

source ${GDB_INIT_FILE_NAME}
source ${PRLCTL_HOME}/gdb/ext/tools.py
GDB_INIT
	)
	echo "${GDB_INIT_CONTENT}" >"${HOME}"/.gdbinit
	echo "${GDB_INIT_CONTENT}" | sudo tee /root/.gdbinit >/dev/null 2>&1
}

base_init() {
	# in my pvms I didn't care about the firewall
	sudo systemctl stop firewalld.service
	sudo systemctl disable firewalld.service
	init_etc_profile
	init_zsh
	init_gdb
	echo "$(hostname -I | awk '{print $1}') $(hostname)" | sudo tee -a /etc/hosts >/dev/null 2>&1
	touch "${PRLCTL_HOME}/shell/$(hostname)${HOME}/already_init_base"
}

case ${1} in
bi | base_init)
	# check if already inited
	if [ ! -f "${PRLCTL_HOME}/shell/$(hostname)${HOME}/already_init_base" ]; then
		base_init
	else
		ynotic "Base init Already done before."
	fi
	;;
rp | recreate_profile)
	# shellcheck disable=1090
	BASE_DIR="$BASE_DIR/deploy/${HOSTNAME}" && . "${BASE_DIR}/init"
	create_profile "${PROFILE}"
	;;
*)
	if [ ! -z ${1} ]; then
		BASE_DIR="$BASE_DIR/deploy/${1}" && . "${BASE_DIR}/init"
	else
		# shellcheck disable=1090
		BASE_DIR="$BASE_DIR/deploy/${HOSTNAME}" && . "${BASE_DIR}/init"
	fi
	base_init
	init
	;;
esac
